import 'dart:async';
import 'dart:math';

import 'package:game/pages/home_page.dart';
import 'package:game/service/network.dart';
import 'package:flutter/material.dart';

class GamePage extends StatefulWidget {
  GamePage();

  @override
  _GamePageState createState() => _GamePageState();
}

class _GamePageState extends State<GamePage>
    with AutomaticKeepAliveClientMixin {
  bool _onStopFlag = false;
  String _info = "请先输入昵称并提交";
  String _name = "";
  int _num1;
  int _num2;
  int _num3;

  TextEditingController _controller;

  bool _inputFlag = false;

  bool _submitFlag = false;

  Timer _timer;

  final _random = Random();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _controller = TextEditingController();
    _num1 = _random.nextInt(6) + 1;
    _num2 = _random.nextInt(6) + 1;
    _num3 = _random.nextInt(6) + 1;
  }

  void _clearState() {
    setState(() {
      _inputFlag = false;
      _submitFlag = false;
      _onStopFlag = false;
      _info = "请先输入昵称并提交";
      _name = "";
      _controller.clear();
    });
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.start,
        children: <Widget>[
          SizedBox(
            height: 30.0,
          ),
          _submitFlag ? _showName(_name) : _NameInput(),
          SizedBox(
            height: 30.0,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.center,
            children: <Widget>[
              TouZi(
                number: _num1,
              ),
              SizedBox(
                width: 20.0,
              ),
              TouZi(
                number: _num2,
              ),
              SizedBox(
                width: 20.0,
              ),
              TouZi(
                number: _num3,
              ),
            ],
          ),
          const SizedBox(
            height: 30.0,
          ),
          StartButton(
            onLongTap: _onPressed,
            child: Text(
              _info,
              style: TextStyle(
                  fontSize: 18.0,
                  fontWeight: FontWeight.bold,
                  color: _onStopFlag ? Colors.black : Colors.grey),
            ),
          ),
          const SizedBox(
            height: 15.0,
          ),
          StartButton(
            onTap: _clearState,
            child: Text(
              "重 置",
              style: TextStyle(
                  fontSize: 18.0,
                  fontWeight: FontWeight.bold,
                  color: Colors.black),
            ),
          ),
        ],
      ),
    );
  }

  void _onPressed() {
    if (!_onStopFlag) return;

    setState(() {
      _info = "等 待 结 束";
    });

    print("开始");
    _timer = Timer.periodic(Duration(milliseconds: 200), (t) {
      setState(() {
        _num1 = _random.nextInt(6) + 1;
        _num2 = _random.nextInt(6) + 1;
        _num3 = _random.nextInt(6) + 1;
        _onStopFlag = false;
      });
    });
    Future.delayed(Duration(seconds: 5), () {
      _timer.cancel();
      setState(() {
        _onStopFlag = false;
        _info = "上报成绩中...";
        report(_name, _num1, _num2, _num3).then((v) {
          setState(() {
            _info = "成绩上报成功";
          });
        });
      });
    });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;

  Widget _NameInput() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Container(
          child: TextField(
            onChanged: _onChange,
            controller: _controller,
            decoration: InputDecoration(
              labelText: "昵称",
            ),
          ),
          width: 200.0,
        ),
        SizedBox(
          width: 10.0,
        ),
        RaisedButton(
          onPressed: _onRegister,
          color: _inputFlag ? Colors.blueAccent : Colors.grey,
          child: Text(
            "提交",
            style: TextStyle(color: Colors.white),
          ),
        )
      ],
    );
  }

  void _onRegister() {
    if (!_inputFlag) return;

    setState(() {
      _info = "摇 骰 子";
      _submitFlag = true;
      _onStopFlag = true;
      _name = _controller.text;
      _controller.clear();
    });
  }

  void _onChange(String value) {
    if (value.isNotEmpty) {
      setState(() {
        _inputFlag = true;
      });
    } else {
      setState(() {
        _inputFlag = false;
      });
    }
  }

  Widget _showName(String text) {
    return Container(
      child: Text(
        "欢迎 $text 加入游戏~",
        style: TextStyle(fontSize: 20.0),
      ),
    );
  }
}

class StartButton extends StatelessWidget {
  final VoidCallback onTap;
  final VoidCallback onLongTap;
  final Widget child;

  StartButton({this.onTap, this.child, this.onLongTap});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onLongPress: onLongTap,
      onTap: onTap,
      child: Container(
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.0),
          border: Border.all(color: Colors.grey, width: 1.0),
          color: Colors.white,
        ),
        width: 310.0,
        height: 60.0,
        child: Center(child: child),
      ),
    );
  }
}
